package com.YCH.article.service.impl;
import com.YCH.article.service.AppLoadUserArticleCategories;
import com.YCH.model.article.dtos.AppUserArticleCategoryDto;
import com.YCH.model.article.pojos.TbArticleCategory;
import com.YCH.model.article.vos.ArticleCategoryCascader;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.common.enums.AppHttpCodeEnum;
import com.YCH.model.mappers.app.TbArticleCategoryMapper;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@SuppressWarnings("all")
public class AppLoadUserArticleCategoriesImpl implements AppLoadUserArticleCategories {
    @Autowired
    private TbArticleCategoryMapper tbArticleCategoryMapper;

    /**
     * 获取用户文章分类列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getUserArticleCategory(AppUserArticleCategoryDto dto) {
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        // 1.先查询文章顶级分类
        List<TbArticleCategory> tbArticleCategories = tbArticleCategoryMapper.selectByUserIdAndParentId(userId, null);
        List<ArticleCategoryCascader> articleCategoryCascaders = new ArrayList<>();
        // 2.遍历顶级分类，逐个取id查询二级分类
        for(TbArticleCategory tbArticleCategory: tbArticleCategories){
            ArticleCategoryCascader articleCategoryCascader = new ArticleCategoryCascader();
            articleCategoryCascader.setId(tbArticleCategory.getId());
            articleCategoryCascader.setName(tbArticleCategory.getName());
            long grandpaId = tbArticleCategory.getId();
            // 3.查询二级分类
            List<TbArticleCategory> tbArticleCategoriesChildren = tbArticleCategoryMapper.selectByUserIdAndParentId(userId, grandpaId);
            List<ArticleCategoryCascader> articleCategoryCascaders1 = new ArrayList<>();
            // 4.遍历二级分类，逐个取id查询三级分类
            for (TbArticleCategory tbArticleCategory1: tbArticleCategoriesChildren){
                ArticleCategoryCascader articleCategoryCascader1 = new ArticleCategoryCascader();
                articleCategoryCascader1.setId(tbArticleCategory1.getId());
                articleCategoryCascader1.setName(tbArticleCategory1.getName());
                // articleCategoryCascader.setChildren(articleCategoryCascader1);
                // ArticleCategoryCascader articleCategoryCascader2 = new ArticleCategoryCascader();
                long parentId = tbArticleCategory1.getId();
                // 二级分类
                List<TbArticleCategory> tbArticleCategoriesGrandson = tbArticleCategoryMapper.selectByUserIdAndParentId(userId, parentId);
                List<ArticleCategoryCascader> articleCategoryCascaders2 = new ArrayList<>();
                for (TbArticleCategory tbArticleCategory2: tbArticleCategoriesGrandson){
                    ArticleCategoryCascader articleCategoryCascader2 = new ArticleCategoryCascader();
                    articleCategoryCascader2.setId(tbArticleCategory2.getId());
                    articleCategoryCascader2.setName(tbArticleCategory2.getName());
                    articleCategoryCascaders2.add(articleCategoryCascader2);
                }
                articleCategoryCascader1.setChildren(articleCategoryCascaders2);
                articleCategoryCascaders1.add(articleCategoryCascader1);
            }
            articleCategoryCascader.setChildren(articleCategoryCascaders1);
            articleCategoryCascaders.add(articleCategoryCascader);
        }

        return ResponseResult.okResult(articleCategoryCascaders);
    }

    @Override
    public ResponseResult getCategory(AppUserArticleCategoryDto dto) {
        // 1.校验参数
        TbUser user = AppThreadLocalUtils.getUser();
        // 用户和用户id不能同时为空
        if (user == null && dto.getUserId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = null;
        if (user != null) {
            userId = user.getId();
        }
        if(dto.getUserId()!=null){
            userId = dto.getUserId();
        }
        List<TbArticleCategory> tbArticleCategories = tbArticleCategoryMapper.selectByUserIdAndParentId(userId, dto.getParentId());
        return ResponseResult.okResult(tbArticleCategories);
    }

    /**
     * 添加用户文章分类
     * @param name
     * @param parentId
     * @return
     */
    @Override
    public ResponseResult addUserArticleCategory(String name ,Long parentId) {
        TbUser user = AppThreadLocalUtils.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Long userId = user.getId();
        // 校验参数
        if(name == null|| "".equals(name)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }if(parentId == null|| parentId < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        TbArticleCategory tbArticleCategory = new TbArticleCategory();
        tbArticleCategory.setUserId(userId);
        tbArticleCategory.setName(name);
        tbArticleCategory.setParentId(parentId);
        Integer insert = tbArticleCategoryMapper.insert(tbArticleCategory);
        return ResponseResult.okResult(insert);
    }

    /**
     * 删除用户文章分类
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteUserArticleCategory(Long id) {
        TbUser user = AppThreadLocalUtils.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Long userId = user.getId();
        if(id == null|| id < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer delete = tbArticleCategoryMapper.delete(id);
        return ResponseResult.okResult(delete);
    }

    /**
     * 更新用户文章分类
     * @param id
     * @param name
     * @return
     */
    @Override
    public ResponseResult updateUserArticleCategory(Long id,String name) {
        TbUser user = AppThreadLocalUtils.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Long userId = user.getId();
        if(id == null|| id < 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(name == null|| "".equals(name)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer update = tbArticleCategoryMapper.update(id, name);
        return ResponseResult.okResult(update);
    }
}
